package com.immo.libcommon.base;

import com.immo.libcommon.R;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 作者： YaoChen
 * 日期： 2017/8/12 10:38
 * 描述： 公用头部的封装
 */
@SuppressWarnings("unused")
public class BaseHeadActivity extends BaseActivity {
    protected TextView titleTv;
    protected TextView titleRight;
    private ImageView titleBack;
    private ImageView titleMore;
    private Toolbar mToolbar;
    private ImageView titleImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_basehead_layout);
        initBaseView();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View.inflate(this, layoutResID, (ViewGroup) findViewById(R.id.base_content));
    }

    /**
     * 调用后 才能得到titleTv否则为空
     */
    private void initBaseView() {
        mToolbar = findViewById(R.id.base_tool_bar);
        setSupportActionBar((Toolbar) findViewById(R.id.base_tool_bar));
        titleTv = findViewById(R.id.base_toolbar_title);
        titleBack = findViewById(R.id.base_nav_back);
        titleRight = findViewById(R.id.base_nav_right);
        titleMore = findViewById(R.id.base_nav_right_img);
        titleImg = findViewById(R.id.base_toolbar_title_img);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        titleRight.setEnabled(true);
        BaseTitleClick baseTitleClick = new BaseTitleClick();
        titleBack.setOnClickListener(baseTitleClick);
        titleRight.setOnClickListener(baseTitleClick);
        titleMore.setOnClickListener(baseTitleClick);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 返回事件
     */
    public void back() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置中间标题
     *
     * @param titleText 标题
     */
    public void setTitle(String titleText) {
        titleTv.setText(titleText);
    }

    /**
     * 设置标题栏是否可见
     */
    public void setTitVisible() {
        mToolbar.setVisibility(View.GONE);
    }

    /**
     * 设置返回按钮的可见性
     */
    public void setBackVisible() {
        titleBack.setVisibility(View.GONE);
    }

    /**
     * @param text          设置右侧按钮文字
     * @param drawableResId 设置右侧的按钮图片
     */
    public void setTitleRight(String text, int drawableResId) {
        titleRight.setVisibility(text != null ? View.VISIBLE : View.GONE);
        titleMore.setVisibility(drawableResId != 0 ? View.VISIBLE : View.GONE);

        if (text != null) {
            titleRight.setText(text);
        }
        if (drawableResId != 0) {
            titleMore.setImageDrawable(getResources().getDrawable(drawableResId));
        }
    }

    /**
     * @param drawableResId 设置标题的按钮图片
     */
    public void setTitleColor(int drawableResId) {
        titleTv.setTextColor(getResources().getColor(drawableResId));
    }

    /**
     * @param drawableResId 设置标题的按钮图片
     */
    public void setTitleImg(int drawableResId) {
        titleImg.setImageDrawable(getResources().getDrawable(drawableResId));
    }

    /**
     * @param drawableResId 设置右侧的按钮图片
     */
    public void setTitleLeft(int drawableResId) {
        titleBack.setImageDrawable(getResources().getDrawable(drawableResId));
    }

    /**
     * 标题按钮的点击事件
     */
    private class BaseTitleClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.base_nav_back) {
                onBackClick();
            } else if (id == R.id.base_nav_right) {
                onRightClick();
            } else if (id == R.id.base_nav_right_img) {
                onRightClick();

            }
        }
    }

    /**
     * 标题中右边的部分，
     */
    protected void onRightClick() {
    }

    /**
     * 返回按钮的点击事件
     */
    private void onBackClick() {
        back();
    }
}
